<?php
    header("Content-type:text/html;charset=utf-8");
    require('./conn/mysql.php');
    $data = $_REQUEST;
    $type = $data['method'] = 'query';
    unset($data['method']);
    try{
        if('add' == $type){
            $res = mysql::exec('user', $data, 'insert');
            if(!$res) die(write(0, '添加失败!'));
            echo write(1, '添加成功!');
        }else if('edit' == $type){
            $id = $data['id'];
            unset($data['id']);
            $res = mysql::exec('user', $data, 'update', '`id`='.$id);
            if(!$res) die(write(0, '修改失败!'));
            echo write(1, '修改成功!');
        }else if('del' == $type){
            $id = $data['id'];
            $sql = 'delete from `user` where `id`='.$id;
            $res = mysql::query($sql);
            if(!$res) die(write(0, '删除失败!'));
            echo write(1, '删除成功!');
        }else{
            $sql = "select count(*) total from `user`";
            $total = mysql::getOne($sql);
            $page = 1;
            $pagesize = 2;
            $pagenum = ceil($total / $pagesize);
            $sql = "select * from `user`";
            $sql .= " limit ". ($page-1) * $pagesize.",".$pagesize;
            $res = mysql::getAll($sql);
            if(!$res) die(write(0, '查询失败!'));
            $data = array(
                'list'=>$res,
                'total'=>intval($total), //总数
                'page'=>intval($page)//当前页
                'limit'=>intval($pagesize),//每页数量
                'pagenum'=>intval($pagenum),//总页数
            );
            echo write(1, '查询成功', $data);
        }
    }catch(\Throwable $t){
        echo write(0, ''.$t);
    }finally{
         mysql::close();
    }

    function write($code, $msg, $data=[]){
        $rd = array(
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        );
        return json_encode($rd, JSON_UNESCAPED_UNICODE);
    }
?>